package com.eshopkeeper.web.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseSysController;
import com.eshopkeeper.core.util.Request2ModelUtil;
import com.eshopkeeper.core.util.WebUtil;
import com.eshopkeeper.model.sys.SysDic;
import com.eshopkeeper.model.sys.SysDicIndex;
import com.eshopkeeper.service.sys.SysDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @author chenshufeng
 * @Description: 字典管理
 * @date 2017年3月7日
 */
@RestController
@Api(value = "字典管理", description = "字典管理")
@RequestMapping(method = RequestMethod.POST)
public class SysDicController extends BaseSysController {
    @Autowired
    private SysDicService sysDicService;

    @ApiOperation(value = "查询字典")
    //@RequiresPermissions("sys.dic.read")
    @RequestMapping(value = "dicIndex/read/list")
    public Object getDicIndex(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        Page<?> list = sysDicService.queryDicIndex(params);
        return setSuccessModelMap(modelMap, list);
    }

    @ApiOperation(value = "字典详情")
    //@RequiresPermissions("sys.dic.detail")
    @RequestMapping(value = "dicIndex/read/detail")
    public Object detail(ModelMap modelMap, @RequestParam(value = "id", required = false) Long id) {
        SysDicIndex record = sysDicService.queryDicIndexById(id);
        return setSuccessModelMap(modelMap, record);
    }

    @ApiOperation(value = "根据关键字查询字典列表")
    //@RequiresPermissions("sys.dic.getDicByKey")
    @RequestMapping(value = "dic/read/key")
    public Object getDicByKey(HttpServletRequest request, ModelMap modelMap,
                              @RequestParam(value = "key", required = false) String key) {
        Map<String, String> result = sysDicService.queryDicByDicIndexKey(key);
        return setSuccessModelMap(modelMap, result);
    }

    @ApiOperation(value = "添加字典")
    //@RequiresPermissions("sys.dic.add")
    @RequestMapping(value = "dicIndex/add", method = RequestMethod.POST)
    public Object addDicIndex(HttpServletRequest request, ModelMap modelMap) {
        SysDicIndex record = Request2ModelUtil.covert(SysDicIndex.class, request);
        sysDicService.addDicIndex(record);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "修改字典")
    //@RequiresPermissions("sys.dic.update")
    @RequestMapping(value = "dicIndex/update", method = RequestMethod.POST)
    public Object updateDicIndex(HttpServletRequest request, ModelMap modelMap) {
        SysDicIndex record = Request2ModelUtil.covert(SysDicIndex.class, request);
        sysDicService.updateDicIndex(record);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "删除字典")
    //@RequiresPermissions("sys.dic.delete")
    @RequestMapping(value = "dicIndex/delete", method = RequestMethod.POST)
    public Object deleteDicIndex(HttpServletRequest request, ModelMap modelMap,
                                 @RequestParam(value = "id", required = false) Long id) {
        sysDicService.deleteDicIndex(id);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "查询字典项")
    //@RequiresPermissions("sys.dic.getDic")
    @RequestMapping(value = "dic/read/list")
    public Object getDic(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        Page<?> list = sysDicService.queryDic(params);
        return setSuccessModelMap(modelMap, list);
    }

    @ApiOperation(value = "字典项详情")
    //@RequiresPermissions("sys.dic.dicDetail")
    @RequestMapping(value = "dic/read/detail")
    public Object dicDetail(ModelMap modelMap, @RequestParam(value = "id", required = false) Long id) {
        SysDic record = sysDicService.queryDicById(id);
        return setSuccessModelMap(modelMap, record);
    }

    @ApiOperation(value = "添加字典项")
    //@RequiresPermissions("sys.dic.addDic")
    @RequestMapping(value = "dic/add", method = RequestMethod.POST)
    public Object addDic(HttpServletRequest request, ModelMap modelMap) {

        SysDic record = Request2ModelUtil.covert(SysDic.class, request);
        sysDicService.addDic(record);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "修改字典项")
    //@RequiresPermissions("sys.dic.updateDic")
    @RequestMapping(value = "dic/update", method = RequestMethod.POST)
    public Object updateDic(HttpServletRequest request, ModelMap modelMap) {
        SysDic record = Request2ModelUtil.covert(SysDic.class, request);
        sysDicService.updateDic(record);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "删除字典项")
    //@RequiresPermissions("sys.dic.deleteDic")
    @RequestMapping(value = "dic/delete", method = RequestMethod.POST)
    public Object deleteDic(HttpServletRequest request, ModelMap modelMap,
                            @RequestParam(value = "id", required = false) Long id) {
        sysDicService.deleteDic(id);
        return setSuccessModelMap(modelMap);
    }
}
